/*
 * @Author: xiaozhuang-c-language 3535851673@qq.com
 * @Date: 2025-06-28 21:21:36
 * @LastEditors: xiaozhuang-c-language 3535851673@qq.com
 * @LastEditTime: 2025-06-29 21:37:57
 * @FilePath: /zhuang/http-v1/mudo/Echo/echo.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include"../source/server.hpp"

class EchoServer
{
 private:
   TcpServer _server;
 public:
    EchoServer(int port,int count,bool enable_inactive_release,int timeout)
    :_server(port)
    {
        //设置线程数量和是否启动非活跃链接超时销毁
        _server.SetThreadCount(count);
        if(enable_inactive_release)
        _server.EnableInactiveRelease(timeout);
       //设置回调
       _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
       _server.SetCloseCallBack(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1)); 
       _server.SetConnectedCallBack(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));       
    }  

    void Start()
    {
        _server.Start();
    }

private:
        void OnConnected(const PtrConnection& conn)
        {
            DBG_LOG("NEW Connection:%p",conn.get());
        }

        void OnClosed(const PtrConnection& conn)
        {
            DBG_LOG("Close Connection:%p",conn.get());
        }

        void OnMessage(const PtrConnection& conn,Buffer* buf)
        {
            conn->Send(buf->ReadPos(),buf->ReadAbleSize());
            buf->MoveReadIdx(buf->ReadAbleSize());
            // conn->ShutDown();
        }
      
};
